package com.jk.controller;

import com.alibaba.fastjson.JSONObject;
import com.jk.bean.Content;
import com.jk.bean.RestClientFactory;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 骚骚的🐏
 * @Author: 杨
 * @Date: 2019/3/8 22:01
 */
@Controller
@RequestMapping("es")
public class EsController {

    @ResponseBody
    @RequestMapping("query")
    public List<Content> query(@RequestBody Content es) throws IOException {
        List<Content> list = new ArrayList();
        SearchRequest searchRequest = new SearchRequest("test");
        SearchSourceBuilder searchsourcebuilder = new SearchSourceBuilder();  //query
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();  //boll
        if(es.getId()==1){
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("btname", es.getText());
            BoolQueryBuilder must = boolQueryBuilder.must(queryBuilder);
            // 设置高亮,使用默认的highlighter高亮器
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.field("btname");
            highlightBuilder.preTags("<span style=\"color:red;font-weight:bold;font-size:15px;\">");
            highlightBuilder.postTags("</span>");
            searchsourcebuilder.from((es.getPagea()-1)*es.getRowsa());
            searchsourcebuilder.size(es.getRowsa());
            searchsourcebuilder.highlighter(highlightBuilder);
            searchsourcebuilder.query(must);


        }else if(es.getId()==2){
            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("authorname", es.getText());
            BoolQueryBuilder must = boolQueryBuilder.must(queryBuilder);
            searchsourcebuilder.query(must);

        }


        searchRequest.source(searchsourcebuilder);

        try {

            SearchResponse search = RestClientFactory.getHighLevelClient().search(searchRequest);
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                String string = hit.getSourceAsString();
                Content content = JSONObject.parseObject(string, Content.class);
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                content.setTotala((int) search.getHits().getTotalHits());
                HighlightField nameField = highlightFields.get("btname");
                if (nameField != null) {
                    content.setBtname(nameField.fragments()[0].toString());
                }
                    list.add(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }


    @ResponseBody
    @RequestMapping("addEs")
    public String addEs(){

        Content es = new Content();
        es.setBtname("aa");
        es.setAuthorid(1);
        es.setAuthorname("aa");
        es.setNameid(1);
        es.setPhoto("aa");
        es.setTablename("aa");
        es.setText("aa");
        //1.获取ES的连接
        RestHighLevelClient client= RestClientFactory.getHighLevelClient();
        //指定存放的位置"
        IndexRequest indexRequest = new IndexRequest("test", "content");
        //book对象-->JSON字符串
        String jsonString = JSONObject.toJSONString(es);
        //存放数据
        indexRequest.source(jsonString, XContentType.JSON);
        //异步操作
        client.indexAsync(indexRequest, new ActionListener<IndexResponse>() {

            @Override
            public void onResponse(IndexResponse indexResponse) {//方法的回调
                System.out.println("异步回调成功");
                System.out.println(indexResponse);
            }
            //调用失败时回调，错误信息作为参数传入
            @Override
            public void onFailure(Exception e) {
                System.out.println(e);
                System.out.println("333333333333333333");
            }
        });

        System.out.println("11111111111111");


        return "";
    }


}
